package com.spc.boot.admin.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.spc.boot.admin.util.DateUtil;
import com.spc.boot.common.security.entity.DbUser;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

/**
 * <p>
 *
 * </p>
 *
 * @author ghliu
 * @since 2019-01-24
 */
@Data
@EqualsAndHashCode
@Accessors(chain = true)
public class SysUser  {

    private static final long serialVersionUID = 1L;

    @TableId(value = "USER_ID", type = IdType.UUID)
    private String userId;

    @TableField("DEPARTMENT_ID")
    private String departmentId;

    @TableField("USER_TYPE")
    private String userType;

    @TableField("LOGIN_ID")
    private String loginId;

    @TableField("PASSWORD")
    private String password;

    @TableField("USER_NAME")
    private String userName;

    @TableField("LOGIN_COUNT")
    private Integer loginCount;

    @TableField("LAST_LOGIN_DATE")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastLoginDate;

    @TableField("LAST_LOGIN_IP")
    private String lastLoginIp;

    @TableField("IS_ENABLED")
    private Boolean isEnabled = true;

    @TableField("IS_LOCKED")
    private Boolean isLocked = false;

    @TableField("LOCK_CHANGE_DATE")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lockChangeDate;

    @TableField("IS_DELETED")
    private Boolean isDeleted = false;

    @TableField("CREATED_DATE")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdDate;

    @TableField("ATTEMPT_LOGIN_TIMES")
    private Integer attemptLoginTimes = 0;

    @TableField("LAST_ATTEMPT_DATE")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastAttemptDate;

    @TableField("MEMO")
    private String memo;

    @TableField("USER_LEVEL")
    private String userLevel;

    // ========= extend
    @TableField(exist = false)
    private String departmentName;

    // 职级
    @TableField(exist = false)
    private String positionLevel;

    public static enum sysUserType {
        内部用户, 外部用户
    }

    public DbUser toDbUser() {
        DbUser entity = new DbUser();
        entity.setUserId(userId);
        entity.setDepartmentId(departmentId);
        entity.setDepartmentName(departmentName);
        entity.setLoginId(loginId);
        entity.setPassword(password);
        entity.setUserName(userName);
        entity.setLoginCount(loginCount);
        entity.setLastLoginIp(lastLoginIp);
        entity.setIsEnabled(isEnabled);
        entity.setIsLocked(isLocked);
        entity.setAttemptLoginTimes(attemptLoginTimes);
        if (lockChangeDate != null) {
            entity.setLockChangeDate(DateUtil.localDateToDate(lockChangeDate.toLocalDate()));
        }
        if (lastLoginDate != null) {
            entity.setLastLoginDate(DateUtil.localDateToDate(lastLoginDate.toLocalDate()));
        }
        if (lastAttemptDate != null) {
            entity.setLastAttemptDate(DateUtil.localDateToDate(lastAttemptDate.toLocalDate()));
        }
        return entity;
    }
}
